package com.newgoldcurrency.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.anythink.banner.api.ATBannerListener;
import com.anythink.banner.api.ATBannerView;
import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.newgoldcurrency.App;
import com.newgoldcurrency.R;
import com.newgoldcurrency.databinding.ActivityNoticeBinding;
import com.newgoldcurrency.utils.Tools;
import com.newgoldcurrency.utils.network.Network;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class NoticeActivity extends AppCompatActivity {
    private final List<Call> calls=new ArrayList<>();
    private ActivityNoticeBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideStatusBar();
        binding = ActivityNoticeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);
        binding.toolbarLayout.setTitle(getString(R.string.notice));
        ActionBar actionbar = getSupportActionBar();
        Glide.with(this).load(getIntent().getStringExtra("image")).into(binding.postDetailImg);
        initBanner();
        if (actionbar!=null) actionbar.setDisplayHomeAsUpEnabled(true);
        HashMap<String, String> map=new HashMap<>();
        map.put("id",getIntent().getStringExtra("id"));
        Network.post(map,"homepage/getNoticeContent", call -> {
            call.enqueue(new Callback(){
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            String str= Objects.requireNonNull(response.body()).string();
                            if (Tools.isJson(str)){
                                JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
                                if (jo.get("message").getAsString().equals("Get notice success")){
                                    JsonObject res = jo.get("data").getAsJsonObject();
                                    binding.noticeTitle.setText(res.get("title").getAsString());
                                    binding.noticeTime.setText(res.get("time").getAsString());
                                    binding.noticeContent.setText(res.get("content").getAsString());
                                }else {
                                    Tools.toast(jo.get("message").getAsString());
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                }
            });
            calls.add(call);
        });
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()) finish();
        return super.onOptionsItemSelected(item);
    }
    private void initBanner() {
        ATBannerView mBannerView = new ATBannerView(this);
        mBannerView.setPlacementId("b620dbae9744a9");
        int width = Tools.screenWidthPixels(this);
        int height =  ViewGroup.LayoutParams.WRAP_CONTENT;
        mBannerView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
        mBannerView.setBannerAdListener(new ATBannerListener() {
            @Override
            public void onBannerLoaded() {
                if (binding!=null)binding.noticeBanner.setVisibility(View.VISIBLE);
            }
            @Override
            public void onBannerFailed(AdError adError) {
            }
            @Override
            public void onBannerClicked(ATAdInfo atAdInfo) {
            }
            @Override
            public void onBannerShow(ATAdInfo atAdInfo) {
            }
            @Override
            public void onBannerClose(ATAdInfo atAdInfo) {
                if (binding!=null)binding.noticeBanner.setVisibility(View.GONE);
            }
            @Override
            public void onBannerAutoRefreshed(ATAdInfo atAdInfo) {
            }
            @Override
            public void onBannerAutoRefreshFail(AdError adError) {
            }
        });
        binding.noticeBanner.addView(mBannerView);
        if (App.ShowBean.hasBanner){
//            binding.noticeBanner.setVisibility(View.VISIBLE);
            mBannerView.loadAd();
        }else {
            binding.noticeBanner.setVisibility(View.GONE);
        }
    }
    private void hideStatusBar() {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        for (Call call:calls) call.cancel();
        binding=null;
    }
}